<?php

function display_bandeau_profil($mysqli,$page) {
    ?>
    <div id="bandeau">
			<div>
				<ul class="menu_band">
					<li><span class="espace_vide"> </span></li><li><a href="profil.php?id=<?php echo $_SESSION['id']; ?>" class="bordure_gauche <?php if($page=="profil") echo "current"; ?>">Voir mon profil</a></li
					><li><a href="messages.php" class="<?php if($page=="message") echo "current"; ?>">Messagerie<?php
                                           $resultg = mysqli_query_with_error($mysqli, "SELECT COUNT(*) as nb FROM privmsgs_thread_gens WHERE user_id='{$_SESSION['id']}' AND status_recept='UNREAD'")->fetch_array();
                                           if ($resultg['nb'] > 0) {
                                               echo " (" . $resultg['nb'] . ")";
                                           }
                                           ?></a></li><li><a href="profiledit.php?id=<?php echo $_SESSION['id']; ?>" class="<?php if($page=="edit") echo "current"; ?> btn btn-vue">Editer mon profil</a></li>
				</ul>
			</div>
    </div>
            <?php
}

function archiver_message($mysqli, $thread_id) {
    $query = "UPDATE privmsgs_thread_gens SET status_envoi='ARCHIVE', status_recept='ARCHIVE' WHERE user_id=" . $_SESSION['id'] . " AND thread_id=" . $thread_id;
    mysqli_query_with_error($mysqli, $query);
}

function supprimer_message($mysqli, $thread_id) {
    $query = "UPDATE privmsgs_thread_gens SET status_envoi='DELETED', status_recept='DELETED' WHERE user_id=" . $_SESSION['id'] . " AND thread_id=" . $thread_id;
    mysqli_query_with_error($mysqli, $query);


    //On vérifie tous les status
    $status = "DELETED";
    $query = "SELECT status_recept From privmsgs_thread_gens WHERE thread_id=" . $thread_id;
    //echo $query.'<br/>';
    $result = mysqli_query_with_error($mysqli, $query);
    while (($ligne = $result->fetch_assoc()) && $status == "DELETED") {

        $status = $ligne['status_recept'];
        //  echo "boucle : ".$ligne['status_recept'].'<br/>';
    }
    //echo 'Résultat'.$status;
    if ($status == 'DELETED') { // Si tous sont DELETED, alors on supprime le thread
        $query = 'DELETE FROM privmsgs_thread WHERE privmsgs_thread_id=' . $thread_id;
        mysqli_query_with_error($mysqli, $query);
    }
}
